<template>
  <div>
    <h1 style="margin-top: 10px">销售订单中心</h1>
    <div style="margin-top: 5px">
      搜索客户订单：
      <el-select filterable
                v-model="customerNo"
                @change="handleSelect(customerNo)"
                style="width:30%">
        <el-option
          v-for="item in customers"
          :key="item.customerNo"
          :label="item.customerName"
          :value="item.customerNo">
          <span style="float: left">{{ item.customerName }}</span>
          <span style="float: right; color: #8492a6; font-size: 13px">{{ item.customerNo }}</span>
        </el-option>
      </el-select>
      <el-button type="primary" @click="search">搜 索</el-button>
      <el-button  @click="cleanSearch">清除搜索</el-button>
    </div>
    <el-container>
      <el-main style="height: 100% ;">
        <div id="table">
          <el-table
            v-loading="loading"
            element-loading-text="拼命加载中"
            :data="oPagedata"
            stripe
            border
            height="690"
            style="width: 100%;height: 100% ;">
            <el-table-column
              prop="orderNo"
              label="订单编号"
              width="180">
            </el-table-column>
            <el-table-column
              prop="customerName"
              label="客户名称"
              width="300">
            </el-table-column>
            <el-table-column
              prop="orderTime"
              label="下单时间"
              width="180">
            </el-table-column>
            <el-table-column
              prop="leadtime"
              label="截止日期"
              width="180">
            </el-table-column>
            <el-table-column
              prop="finalTotal"
              label="订单金额"
              width="180">
            </el-table-column>
            <el-table-column prop="orderStatus" label="订单状态" width="180">
              <template slot-scope="{row: {orderStatus}}">
                <span v-if="+orderStatus === 0">生成中</span>
                <span v-else-if="+orderStatus === 3">已取消</span>
                <span v-else-if="+orderStatus === 7">已出库</span>
                <span v-else>完成</span>
              </template>
            </el-table-column>
            <el-table-column
              label="操作">
              <template slot-scope="scope">
                <el-row>
                  <el-col :span="4"><el-button type="text" size="small" @click="goOrderDetail(scope.row)">查看</el-button></el-col>
                  <el-col :span="4"><el-button type="text" size="small" @click="goPrint(scope.row)">打印</el-button></el-col>
                </el-row>
                <el-row>
                  <el-col :span="4"><el-button type="text" :disabled ="+scope.row.orderStatus===0?false:true" size="small"  @click="cSta3( scope.row )" v-if="status==='1'">取消</el-button></el-col>
                  <el-col :span="4"><el-button type="text" :disabled ="+scope.row.orderStatus===0?false:true" size="small" @click="outsto( scope.row )" v-if="status==='1'">出库</el-button></el-col>
                </el-row>
                <el-row>
                  <el-col :span="4"><el-button type="text" :disabled ="+scope.row.orderStatus===7?false:true" size="small"   @click="cSta9( scope.row )" v-if="status==='1'">发货</el-button></el-col>
                </el-row>
              </template>
            </el-table-column>
          </el-table>
          <el-pagination
            layout="prev, pager, next"
            background
            @current-change="pageChange"
            :current-page.sync="currentPage"
            :total='dataNum'>
          </el-pagination>
        </div>
      </el-main>
      <el-footer>
        <el-col :span="4" :offset="20"><el-button type="primary" @click="creatorder()">创建订单</el-button></el-col>
      </el-footer>
    </el-container>
  </div>
</template>

<script>
  export default {
    name: "order",
    inject:['reload'],
    data(){
      return {
        customers:[],
        customerNo: '',
        loading: true,
        status:'',
        odata: [],
        oPagedata: [],
        dataNum:0,
        currentPage:1,
        OrDetaildata: [],
        flag:'',
        mes:'',
      }
    },
    created(){
      this.xuanran()
    },
    methods:{
      pageChange(val){
        this.loading=true;
        let pageNo=this.currentPage
        this.oPagedata=[]
        for(let i=(pageNo-1)*10;i<(pageNo*10>this.dataNum?this.dataNum:pageNo*10);i++){
          this.oPagedata.push(this.odata[i])
        }
        setTimeout(()=>{
          this.loading=false
        },400);
      },
      goPrint(item){
        this.$router.push({
          path:"/print",
          query:{
            "orderNo":item.orderNo,
            "customerNo":item.customerNo
          }
        })
      },
      search(){
        this.$http.post("/hnhyscms_ws/orders/getByCustomerNo",{customerNo:this.customerNo},{emulateJSON:true}).then(
          success=>{
            this.odata=success.data
            this.dataNum=success.data.length
            this.pageChange()
          }
        )
      },
      cleanSearch(){
        this.customerNo=""
        this.xuanran()
      },
      xuanran(){
        this.status=localStorage.getItem("status")
        this.$http.get("/hnhyscms_ws/orders/getAllOrders",{emulateJSON:true}).then(
          success=>{
            this.odata=success.data
            this.dataNum=success.data.length
            this.pageChange()
          }
        )
        this.$http.get("/hnhyscms_ws/customers/getAllCustomers",{emulateJSON:true}).then(
          success=>{
            this.customers=success.data;
          })
      },
      creatorder(){
        this.$router.push({
          path:"/createorder"
        })
      },
      goOrderDetail(item){
        this.$router.push({
          path:"/orderdetail",
          query:{
            "orderNo":item.orderNo,
            "customerNo":item.customerNo
          }
        })
      },
      async cSta3(item){
        let param={
          "orderNo":item.orderNo,
          'orderStatus':'3'
        }
        await this.$http.post("/hnhyscms_ws/orders/updateType", param,{emulateJSON: true}).then(
          sucess => {})
        this.reload();
      },
      async cSta7(item){
        let param={
          "orderNo":item.orderNo,
          'orderStatus':'7'
        }
        await this.$http.post("/hnhyscms_ws/orders/updateType", param,{emulateJSON: true}).then(
          sucess => {})
        this.reload();
      },
      async cSta9(item){
        let param={
          "orderNo":item.orderNo,
          'orderStatus':'9'
        }
        await this.$http.post("/hnhyscms_ws/orders/updateType", param,{emulateJSON: true}).then(
          sucess => {})
        this.reload();
      },
      checkSto(ckparams){
        return this.$http.post("/hnhyscms_ws/cpoutsto/checkSto", ckparams,{emulateJSON: true}).then(
            sucess => {
              if (sucess.data == "1") {
                this.flag = 1;
              }
            }
          );
      },
      outsto(item) {
        let param={
          "orderNo":item.orderNo,
        }
        this.$http.post("/hnhyscms_ws/orderdetails/getByOrderNo",param, {emulateJSON: true}).then(
          async success => {
            this.flag = 0;
            this.OrDetaildata = success.data;
            for (let i in this.OrDetaildata) {
              let ckparams = {
                "productNo": this.OrDetaildata[i].productNo,
                "amount": this.OrDetaildata[i].amount
              }
              await this.checkSto(ckparams);
              if (this.flag == 1)
              this.mes='出库失败（原因：'+this.OrDetaildata[i].productName+'等库存不足）'
              {
                this.$message({
                  message:this.mes,
                  type: 'warning'
                });
                break;
              }
            }
            if(this.flag==0){
              for (let i in this.OrDetaildata) {
                let params = {
                  "productNo": this.OrDetaildata[i].productNo,
                  "amount": this.OrDetaildata[i].amount,
                  "orderNo": this.OrDetaildata[i].orderNo
                }
                this.$http.post("/hnhyscms_ws/cpoutsto/addCpoutsto", params, {emulateJSON: true}).then(success => {})
              }
              this.$message({
                message: '出库成功',
                type: 'success'
              });
              this.cSta7(item);
            }
          }
        )
      },
    }
  }
</script>

<style scoped>

</style>
